package com.xopencode.io;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

/**
 * @author Mcb
 * @date 2022/5/2
 * @apiNote
 */
public class BufferTest {
    public static void main(String[] args) {
        String info = "This is my test info";
        ByteBuffer buffer = ByteBuffer.allocate(512);
        ByteBuffer buffer2 = ByteBuffer.allocate(512);
        buffer.put(info.getBytes(StandardCharsets.UTF_8));
        buffer2.put(info.getBytes(StandardCharsets.UTF_8));
        buffer.flip();
        buffer2.flip();
        for01(buffer);
        for02(buffer2);
    }

    public static void for01(ByteBuffer buffer) {
        while (buffer.hasRemaining()) {
            byte b = buffer.get();
            System.out.println((char) b);
        }
    }

    public static void for02(ByteBuffer buffer) {
        byte[] datas = new byte[buffer.limit()];
        buffer.get(datas);
        System.out.println(new String(datas));
    }
}
